﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SketchUpAPI.NET.Objects.Tables
{
    /// <summary>
    /// 线型表
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SULineStylesRef 的包装</remarks>
    public class LineStyleTable : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取线型个数
        /// </summary>
        /// <param name="line_styles"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStylesGetNumLineStyles(IntPtr line_styles, out int count);

        /// <summary>
        /// 获取所有线型的名字
        /// </summary>
        /// <param name="line_styles"></param>
        /// <param name="len"></param>
        /// <param name="line_styles_provider_names"></param>
        /// <param name="realcount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStylesGetLineStyleNames(IntPtr line_styles, int len, IntPtr[] line_styles_provider_names, out int realcount);

        /// <summary>
        /// 使用名字获取线型
        /// </summary>
        /// <param name="line_styles"></param>
        /// <param name="name"></param>
        /// <param name="line_style"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStylesGetLineStyleByName(IntPtr line_styles, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, out IntPtr line_style);
        #endregion

        /// <summary>
        /// 线型个数
        /// </summary>
        public int Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStylesGetNumLineStyles(Id, out int count));
                return count;
            }
        }

        /// <summary>
        /// 所有线型的名字
        /// </summary>
        public List<string> Names
        {
            get
            {
                IntPtr[] pts = new IntPtr[Count];
                SketchUpExceptionHelper.ThrowSUResult(SULineStylesGetLineStyleNames(Id, pts.Length, pts, out _));
                return pts.Select(i => new StringRef() { Id = i }.Dispose()).ToList();
            }
        }

        /// <summary>
        /// 使用名字获取线型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public LineStyle this[string name]
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStylesGetLineStyleByName(Id, name, out IntPtr s));
                return new LineStyle(s);
            }
        }

        internal LineStyleTable(IntPtr id) => Id = id;
    }
}